package cate.game.play.buff.h;

import cate.common.table.d.GDSkill;
import cate.game.play.base.TargetSelector;
import cate.game.play.buff.BuffHandler;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.skill.FighterCD;
import cate.game.play.util.FighterFilter;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 死亡后添加buff
 */
public class 死亡添加buffBH extends BuffHandler {

	private List<Config> configs =new ArrayList<>();

	@Override
	public void parse(String code) {
		if(StringUtils.isNotBlank(code)){
			for (String s : code.split(",")) {
				configs.add(new Config(s));
			}
		}
	}

	@Override
	public void onDead(ActionCtx action, Fighter killer) {
		for (Config config : configs) {
			config.settle(action, buff.owner);
		}
	}

	public static class Config{

		private PlayCfgArgs args;

		private int buff;

		private int 数量;

		private byte 敌我目标;

		private int 冷却回合;

		//id=??&数量=1&敌我目标=2&冷却回合=15
		public void parse(String code) {
			args = new PlayCfgArgs(code);
			buff = args.getInt("id", 0);
			数量 = args.getInt("数量", 1);
			敌我目标 = args.getByte("敌我目标", GDSkill.Scope.SELF);
			冷却回合 = args.getInt("冷却回合", 0);
		}

		public Config(String code) {
			parse(code);
		}

		private int lastSettleRound;

		public void settle(ActionCtx action, Fighter owner) {
			if (FighterCD.inCD(lastSettleRound, 冷却回合, action.getRound().index)) {
				return;
			}

			lastSettleRound = action.getRound().index;
			List<Fighter> targets = TargetSelector.findByScope(action.getTeamVs(),owner, 敌我目标);
			for (Fighter target : targets) {
				if (FighterFilter.test(args, owner, target)) {
					target.buff.tryAddSome(action, owner, buff, 数量);
				}
			}
		}
	}
}
